#include "ccu6_compare.h"
#include "motor_ctrl.h"

static uint16 test_cnt;
static uint16 phase_cnt;
static uint16 voltage_cnt;
static uint16 voltage_freq = 50;
static uint16 voltage_duty = 1120;
static uint16 voltage_freq_init = 260;
static uint16 voltage_duty_init = 1180;
//static uint16 first_start = 0x01;

motor_param_t motor_param =	{MOTOR_PRE_POSITION, MOTOR_CCW,PHASE_CHANGE ,1 ,0 ,0,0 };


void CCU6_COMPARE_CB(void)
{
	switch(motor_param.motor_status)   										
	{
		case MOTOR_PRE_POSITION:
			switch(motor_param.step)
			{
				case 1:
					up_vn_wn_pwm(1180);
					if(++test_cnt > 400)
					{
						test_cnt = 0;
						motor_param.step = 2;
					}
				break;
				case 2:
					up_vn_wn_pwm(1180);
					if(++test_cnt > 200)
					{
						test_cnt = 0;
						motor_param.motor_status = MOTOR_OPEN_ACC;
						motor_param.step = 1;
						motor_param.phase = voltage_freq_init;
						motor_param.voltage = voltage_duty_init;
					}					
				break;
			}
		break;
		case MOTOR_OPEN_ACC:

			 switch(motor_param.change_phase)
			{
				case PHASE_CHANGE:
					if(motor_param.phase <= voltage_freq)
					{
						motor_param.phase = voltage_freq;
//						phase_cnt++;
//						if(phase_cnt >= 10 && phase_cnt < 20)
//						{
//							voltage_freq = 39;
//						}
//						else if(phase_cnt >= 30 && phase_cnt < 40)
//						{
//							voltage_freq = 38;
//						}
//						else if(phase_cnt >= 50 && phase_cnt < 60)
//						{
//							voltage_freq = 37;
//						}
//						else if(phase_cnt >= 70 && phase_cnt < 80)
//						{
//							voltage_freq = 36;
//						}
//						else if(phase_cnt >= 80 && phase_cnt < 90)
//						{
//							voltage_freq = 35;
//						}
//						else if(phase_cnt >= 100 && phase_cnt < 110)
//						{
//							voltage_freq = 34;
//						}
//						else if(phase_cnt >= 110 && phase_cnt < 120)
//						{
//							phase_cnt = 150;
//							voltage_freq = 33;
//						}
					//motor_param.phase -= motor_param.phase / MOTOR_PHASE_50HZ;
					}
					else
					{
						motor_param.phase -= motor_param.phase / 33;		
						//motor_param.phase -= 3;
						
					}
					if(motor_param.voltage <= MOTOR_MAX__VOLTAGE)
					{
						motor_param.voltage = MOTOR_MAX__VOLTAGE;
					//motor_param.voltage += motor_param.voltage / MOTOR_MAX__VOLTAGE;	
					}
					else
					{
						motor_param.voltage -= 3;
					}
					if(motor_param.step++ > 6)
					{
						motor_param.step = 1;
					}
					if(MOTOR_CCW == motor_param.driction)
					{
						switch(motor_param.step)
						{
							case 1:
								up_vn_wn_pwm(motor_param.voltage);
								motor_param.change_phase = PHASE_WAIT;
							break;
							case 2:
								up_vp_wn_pwm(motor_param.voltage);
								motor_param.change_phase = PHASE_WAIT;
							break;
							case 3:
								un_vp_wn_pwm(motor_param.voltage);
								motor_param.change_phase = PHASE_WAIT;
							break;
							case 4:
								un_up_wp_pwm(motor_param.voltage);
								motor_param.change_phase = PHASE_WAIT;
							break;
							case 5:
								un_vn_wp_pwm(motor_param.voltage);
								motor_param.change_phase = PHASE_WAIT;
							break;
							case 6:
								up_vn_wp_pwm(motor_param.voltage);
								motor_param.change_phase = PHASE_WAIT;
							break;
						}
					}
					else
					{
					}
				break;
				case PHASE_WAIT:
					if(motor_param.phase_time++ > motor_param.phase)
					{
						motor_param.phase_time = 0;
						motor_param.change_phase = PHASE_CHANGE;
					}
		  }
		break;
	}
	
}






